package mesweb.mesweb.controller.procedure;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.Procedure.ModelProductProcedureDispatch;
import mesweb.mesweb.service.procedure.IModelProductProcedureDispatchService;
import mesweb.mesweb.vo.procecdure.ModelProductProcedureDispatchVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 产品线下发
 * @Author: jeecg-boot
 * @Date: 2021-07-04
 * @Version: V1.0
 */

@Api(tags = "产品线下发")
@RestController
@RequestMapping("/mes/modelProductProcedureDispatch")
public class ModelProductProcedureDispatchController {
    @Autowired
    private IModelProductProcedureDispatchService modelProductProcedureDispatchService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam(value = "name", required = false) String name) {
        List<ModelProductProcedureDispatchVo> data = new ArrayList<>();
        QueryWrapper<ModelProductProcedureDispatchVo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
        }
        data = modelProductProcedureDispatchService.getProductProcedureDispatch(queryWrapper);
        return CommonResult.success(data);
    }

    /**
     * 分页列表查询
     *
     * @param modelProductProcedureDispatchVo
     * @param page
     * @param size
     * @param req
     * @return
     */
    @ApiOperation(value = "产品线下发-分页列表查询", notes = "产品线下发-分页列表查询")
    @GetMapping(value = "/list")
    public CommonResult<?> queryPageList(ModelProductProcedureDispatchVo modelProductProcedureDispatchVo,
                                         @RequestParam(name = "page", defaultValue = "1") Integer page,
                                         @RequestParam(name = "size", defaultValue = "10") Integer size,
                                         HttpServletRequest req) {
        QueryWrapper<ModelProductProcedureDispatchVo> queryWrapper = new QueryWrapper<>();
        if (modelProductProcedureDispatchVo != null) {
            if ( null != modelProductProcedureDispatchVo.getProduceLineId() && modelProductProcedureDispatchVo.getProduceLineId() > 0) {
                queryWrapper.eq("a.produce_line_id", modelProductProcedureDispatchVo.getProduceLineId());
            }
        }
        Page<ModelProductProcedureDispatchVo> pageModelProductProcedureDispatchVo = new Page<ModelProductProcedureDispatchVo>(page, size);
        IPage<ModelProductProcedureDispatchVo> pageList = modelProductProcedureDispatchService.getProductProcedureDispatch(pageModelProductProcedureDispatchVo, queryWrapper);
        return CommonResult.success(pageList);
    }

    /**
     * 编辑
     *
     * @param modelProductProcedureDispatch
     * @return
     */
    @ApiOperation(value = "产品线下发-编辑", notes = "产品线下发-编辑")
    @PostMapping(value = "/save")
    public CommonResult<?> edit(@RequestBody ModelProductProcedureDispatch modelProductProcedureDispatch) {
        boolean result = false;
        if (modelProductProcedureDispatch.getId() == 0L) {
            result = modelProductProcedureDispatchService.save(modelProductProcedureDispatch);
        } else {
            result = modelProductProcedureDispatchService.updateById(modelProductProcedureDispatch);
        }
        return CommonResult.success(result);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "产品线下发-通过id删除", notes = "产品线下发-通过id删除")
    @DeleteMapping(value = "/delete")
    public CommonResult<?> delete(@RequestParam(name = "id", required = true) String id) {
        modelProductProcedureDispatchService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "产品线下发-批量删除", notes = "产品线下发-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public CommonResult<?> deleteBatch(@ApiParam("ModelProductProcedureDispatchIDs") @RequestBody List<Long> ids) {
        this.modelProductProcedureDispatchService.removeByIds(ids);
        return CommonResult.success("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "产品线下发-通过id查询", notes = "产品线下发-通过id查询")
    @GetMapping(value = "/queryById")
    public CommonResult<?> queryById(@RequestParam(name = "id", required = true) String id) {
        ModelProductProcedureDispatch modelProductProcedureDispatch = modelProductProcedureDispatchService.getById(id);
        return CommonResult.success(modelProductProcedureDispatch);
    }
}
